package com.qf.shopping.service.impl;


import com.qf.shopping.dto.systemDTO.UserDTO;
import com.qf.shopping.dto.systemDTO.UserProfileDTO;
import com.qf.shopping.mapper.IAdminMangerMapper;

import com.qf.shopping.pojo.System.User;
import com.qf.shopping.service.AdminMangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class AdminMangerServiceImpl implements AdminMangerService {

    @Autowired
    private IAdminMangerMapper adminMangerMapper=null;
    @Override
    public List<UserDTO> getUserByAll() {
        List<UserDTO> listDTO=new ArrayList<>();
        List<User> list =adminMangerMapper.findUserByAll();
        for (User u:list) {
            listDTO.add(new UserDTO(u));
        }
        return listDTO;
    }

    @Override
    public UserProfileDTO findUserById(long id) {
        User u =adminMangerMapper.findUserById(id);
        UserProfileDTO userDTO=new UserProfileDTO(u);
        return userDTO;
    }

    @Override
    public List<User> findUserByNamelike(String name) {
        return null;
    }

    @Override
    public void addAdmin(UserProfileDTO user) {
        User u =new User();
        u.setId(user.getId());
        u.setUserName(user.getUserName());
        u.setPassword(user.getPassword());
        u.setName(user.getName());
        u.setSalt(user.getSalt());
        u.setMobile(user.getMobile());
        u.setEmail(user.getEmail());
        u.setSalt(user.getSalt());
        u.setConsumer(user.getSalt());
        u.setRemark(user.getRemark());
        u.setIsSuper(user.getIsSuper());
        u.setDept(user.getDept());
        adminMangerMapper.addAdmin(u);
    }

    //修改
    @Override
    public void updateAdmin(UserProfileDTO user) {
        User u=adminMangerMapper.findUserById(user.getId());
        u.setUserName(user.getUserName());
        u.setPassword(user.getPassword());
        u.setName(user.getName());
        u.setSalt(user.getSalt());
        u.setMobile(user.getMobile());
        u.setEmail(user.getEmail());
        u.setSalt(user.getSalt());
        u.setConsumer(user.getSalt());
        u.setRemark(user.getRemark());
        u.setDept(user.getDept());
        adminMangerMapper.updateAdmin(u);
    }

    @Override
    public void deleteAdmin(int id) {
        adminMangerMapper.deleteAdmin(id);
    }

    @Override
    public void updateUser(UserProfileDTO user) {
        User u=adminMangerMapper.findUserById(user.getId());
        u.setEmail(user.getEmail());
        u.setMobile(user.getMobile());
        adminMangerMapper.updateAdmin(u);
    }




}